package com.example.hw_demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hw_demo.entity.PageObj;
import com.example.hw_demo.entity.User;
import com.example.hw_demo.service.UserService;
import com.example.hw_demo.util.Result;
import com.example.hw_demo.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;
    @GetMapping("/test")
    public Result test(){
        return new Result(200,"测试查询成功",userService.list());
        //return  userService.list();
    }


    @GetMapping ("/selectPage")
    public Result selectPage( User user){
        return userService.selectPage(user);

    }


    @PostMapping("/login")
    public Result login(@RequestBody User user){
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("user_account",user.getUserAccount())
                .eq("user_password",user.getUserPassword());
        User user1 = userService.getOne(queryWrapper);//单个对象查询
        //判断是否有值
        if(user1!=null){
            //登录成功
            String tokenString = new TokenUtil().createToken(user1);
            return  new Result(200,"登录成功",tokenString);
        }else{
            //登录失败
            return  new Result(500,"账号密码有误",null);
        }
    }
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        return new Result(200,"新增成功",userService.save(user));
    }



}
